import cupk.abner.Phone;
import cupk.abner.PhoneBuilder;
import cupk.abner.PhoneDirector;
import cupk.abner.concreteBuilder.HuaweiP70;
import cupk.abner.concreteBuilder.Iphone15;
import cupk.abner.concreteBuilder.Xiaomi14;
import org.w3c.dom.NodeList;
import org.w3c.dom.Document;
import org.w3c.dom.Node;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        try {
            List<String> PhoneClassNames = ReadClassNameFromXML();
            for(String PhoneClassName : PhoneClassNames){
                //反射机制
                Class classReflection = Class.forName(PhoneClassName);
                //创建对象
                Object objectReflection = classReflection.newInstance();
                /制类型转换
                PhoneBuilder builder = (PhoneBuilder) objectReflection;
                //调用方法
                PhoneDirector director = new PhoneDirector(builder);
                //调用产品方法
                Phone myPhone = director.concretePhone();
                System.out.println(myPhone.toString());
                System.out.println("-------------------");
            }
        }catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
    public static List<String> ReadClassNameFromXML(){
        List<String> classNames = new ArrayList<>();
        try{
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = builderFactory.newDocumentBuilder();
            Document doc;
            doc = builder.parse(new File("src//config.xml"));

            NodeList nl = doc.getElementsByTagName("className");
            for(int i = 0;i < nl.getLength();i++){
                Node classNode = nl.item(i).getFirstChild();
                classNames.add(classNode.getNodeValue());
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return classNames;
    }
}